<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .input-box {
            margin: 10px;
            display: flex;
            align-items: center;
        }

        .input-box input {
            flex: 1;
            height: 40px;
            border: 1px solid #ccc;
            outline: none;
            padding-left: 20px;
            border-radius: 5px;
        }

        .input-box span {
            text-align: center;
            color: lightseagreen;
            width: 60px;
        }

        .list {
            margin: 10px;
        }

        .list .task {
            margin: 20px 0;
            display: flex;
            justify-content: space-between;
            padding: 15px;
            background-color: red;
            color: white;
        }

        .list .task .finished {
            background-color: lightseagreen;
        }

        .empty {
            text-align: center;
            margin: 50px 0;
            color: red;
        }

        .box {
            margin: 10px;
            display: flex;
            justify-content: space-around;
        }

        .box div {

            height: 40px;
            background-color: lightseagreen;

        }

        .box .undo {
            background-color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="input-box">
            <input type="text" placeholder="输入任务名称" v-model="inputText">
            <span @click="add">新增</span>
        </div>
        <div class="box">
            <div>全部任务({{tasklist.length}})</div>
            <div>已完成({{finishText()}})</div>
            <div class="undo">未完成({{undoText()}})</div>
        </div>
        <div class="list">
            <div :class="{task:true,finished:item.status}" v-for="(item,index) in tasklist" :key="index">
                <input type="checkbox" v-on:click="changeStatus(item)">
                <div class="name">{{item.name}}</div>
                <span class="btn" @click="delete(index)">删除</span>
            </div>
        </div>
        <div class="empty" v-show="tasklist.length == 0">任务列表空空如也！</div>
    </div>
    <script src="./vue-2.5.21.js"></script>
</body>

</html>
<script>
    var v = new Vue({
        el: '#app',
        data: {
            inputText: '',
            tasklist: [

            ]
        },
        methods: {
            add() {
                if (this.inputText) {
                    var obj = { id: this.tasklist.length + 1, name: this.inputText, status: false }
                    this.tasklist.push(obj)
                    this.inputText = ''
                }
            },

            changeStatus(item) {
                item.status = !item.status
            },
            delete(index) {
                this.tasklist.splice(index, 1)
            },
            finishText() {
                var finishcount = 0
                this.tasklist.forEach((item, index) => {
                    if (item.status == true) {
                        finishcount++
                    }
                })
                return finishcount
            },
            undoText() {
                var undocount = 0
                this.tasklist.forEach((item, index) => {
                    if (item.status == false) {
                        undocount++
                    }
                })
                return undocount
            }
        }
    })
</script>